/* 
 *  @treasure Product
 *  Author: Alex Hu
 *
 */

'use strict';

GML.Treasures['manage-product'] = {
    "mine": function(config){
        var preLabel
            , RT = GML.RT
            , label = config.label
            , labelList = config.labelList
            ;

        if(!label){
            label = labelList[0];
            preLabel = label;
        }

        RT.html(GML.Template.tube["manage-product"](label));

        var tabName = [ "online", "offline", "unapproved", "auditing", "deleted" ]
            , manage = []
            , productURL = GML.URL.product
            , tab = $("#tabList [role=tab]");
            ;

        var old_product_url = "/gmvo/mygm/product/producttools/"
            , refreshMSG = function($this, checkedItems, config){
                config.FN.refresh();
            }
            , notice = function(){
                return function(type, statusMark){
                    var operation
                        , operationCode = 133
                        , statusCode = statusMark ? 171 : 170
                        ;

                    switch(type){
                        case "deleted":
                            operationCode = 56;
                            break;
                        case "assigned":
                            operationCode = 56;
                            break;
                        case "new":
                            operationCode = 232;
                            break;
                        case "offline":
                            operationCode = 72;
                            break;
                    }
                    operation = T(operationCode);
                    return T(statusCode, type);
                }
            }
            , offline_G_MSG = {
                "key": "offline-G-MSG",
                "line": old_product_url + "bulkOffline.gm",
                "execute": {
                    "success": function($this, checkedItems, config){
                        refreshMSG($this, checkedItems, config);
                        $("#offline_tab").data("refresh", true);
                    }
                },
                "notice": {
                    "success": notice("offline")
                }
            }
            , del_G_MSG = {
                "key": "del-G-MSG",
                "line": old_product_url + "bulkDelete.gm",
                "execute": {
                    "success": function($this, checkedItems, config){
                        refreshMSG($this, checkedItems, config);
                        $("#deleted_tab").data("refresh", true);
                    }
                },
                "notice": {
                    "success": notice("deleted")
                }
            }
            , assign_G_MSG = {
                "key": "assign-G-MSG",
                "line": productURL + "assign.gm",
                "execute": {
                    "success": function($this, checkedItems, config){
                        console.log(checkedItems);
                    }
                },
                "notice": {
                    "success": notice("assigned")
                }
            }
            , new_G_MSG = {
                "key": "new-G-MSG",
                "line": old_product_url + "bulkMarker.gm?markerName=New+Product&type=1",
                "execute": {
                    "success": function($this, checkedItems, config){
                        var i, item, typeCol
                            , newClassName = "mark-new"
                            ;

                        for(i=0; i<checkedItems.length; i++){
                            item = $(checkedItems[i]);
                            typeCol = item.find("> .cat");
                            if(!typeCol.find("." + newClassName).length){
                                typeCol.append(
                                    $("<div />", {
                                        "class": newClassName,
                                        text: T(226)
                                    })
                                );
                            }
                        }
                    },
                    "fail": function($this, checkedItems, config){
                    }
                },
                "notice": {
                    "success": notice("new"),
                    "fail": notice("new", "fail")
                }
            }
            , unnew_G_MSG = {
                "key": "unnew-G-MSG",
                "line": old_product_url + "bulkRmMarker.gm?markerName=New+Product&type=1",
                "execute": {
                    "success": refreshMSG
                },
                "notice": {
                    "success": function($this){
                        return "unnew successfully"; // T(12, T(3));
                    }
                }
            }
            // CN => choiceness
            , CN_G_MSG = {
                "key": "CN-G-MSG",
                "line": old_product_url + "bulkMatch.gm?markerName=Selected+Product",
                "execute": {
                    "success": refreshMSG
                },
                "notice": {
                    "success": function($this){
                        return "choiceness successfully"; // T(12, T(3));
                    }
                }
            }
            // unCN => un choiceness
            , unCN_G_MSG = {
                "key": "unCN-G-MSG",
                "line": old_product_url + "bulkRmMatch.gm?markerName=Selected+Product",
                "execute": {
                    "success": refreshMSG
                },
                "notice": {
                    "success": function($this){
                        return "unChoiceness successfully"; // T(12, T(3));
                    }
                }
            }
            , online_G_MSG = {
                "key": "online-G-MSG",
                "line": old_product_url + "bulkOnline.gm",
                "execute": {
                    "success": refreshMSG
                },
                "notice": {
                    "success": function($this){
                        return "online successfully"; // T(12, T(3));
                    }
                }
            }
            , restore_U_MSG = {
                'key': 'restore-U-MSG',
                "plus": {
                    'line': productURL + 'restore.gm',
                    "notice": notice("restore")
                },
                /*
                "minus": {
                    'line': starURL + 'unmark',
                    "notice": notice("unstar")
                },
                */
                "execute": function(){
                    // toggleUnit()
                }
            }
            ;

        var renderMSG = function(label, el){
            switch(label){
                case "online":
                    manage.push(offline_G_MSG, del_G_MSG, assign_G_MSG, new_G_MSG, unnew_G_MSG, CN_G_MSG, unCN_G_MSG);
                    break;
                case "offline":
                    manage.push(online_G_MSG, del_G_MSG);
                    break;
                case "unapproved":
                    break;
                case "auditing":
                    break;
                case "deleted":
                    manage.push(restore_U_MSG);
                    break;
            }
            GML.Msg.render({
                url: productURL + "manage.gm",
                // 'first': URL,
                label: label,
                line: productURL + "list.gm?label=" + label,
                namespace: {
                    mid: "productIds",
                    page: "page"
                },
                recognize: [ "id" ],
                template: {
                    nothing: "nothing-message",
                    without: "without-message",
                    list: "list-product"
                },
                /*
                shortcutPanel: {
                    "class": "bg-icon ico-close-s2",
                    role: SCRole
                },
                */
                manage: manage
            });
            el.data("render", true);
        }

        var switchTab = function(el){
            return function(){
                var label = el.data("label");

                if(preLabel === label){
                    return;
                }

                if(el.data("refresh")){
                    GML.Msg.fire($("#boss_" + label + "_MSG"));
                    el.data("refresh", false);
                }else if(!el.data("render")){
                    renderMSG(label, el);
                    // el.data("render", true);
                }
                preLabel = label;
            }
            /*
            return function(){
                // if(d)
            };
            */
        }

        var updateNO = function(){
            $.ajax({
                url: "/gmvo/product/loadProductCount.gm",
                success: function(d) {
                    if(d["status"] !== "success"){
                        return;
                    }
                    var i, item, text, NO
                        , data = d["data"]
                        ;

                    for(i=0; i<tabName.length; i++){
                        item = tabName[i];
                        NO = data[item];
                        text = NO ? "(" + NO + ")" : "";
                        if(data[item]){
                            $(".no ", "#" + item + "_tab").text(text);
                        }
                    }
                }
            });
        };

        tab.each(function(index){
            var $this = $(this);

            $this.data("callback", switchTab($this));
        });

        renderMSG(label, tab.filter(".cur"));
        updateNO();

    }
}

